package com.zrq.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DemoAdapter extends Adapter<ViewHolder> {

    public static  final int TYPE_ONE=1;
    public static  final int TYPE_TWO=2;
    public static  final int TYPE_THREE=3;


    private LayoutInflater mInflater;


    public DemoAdapter(Context context) {
        mInflater=LayoutInflater.from(context);
    }


    /**
     * 使用不同的数据结构来存储数据，做出响应处理。
     */
    private List<DataModelOne> list1;
    private List<DataModelTwo> list2;
    private List<DataModelThree> list3;
    //存储type的数组
    private List<Integer> types=new ArrayList<>();
    private Map<Integer,Integer> mPositions=new HashMap<>();

    public void addList(List<DataModelOne>list1,List<DataModelTwo>list2,List<DataModelThree>list3){
        addList(TYPE_ONE,list1);
        addList(TYPE_TWO,list2);
        addList(TYPE_THREE,list3);
        this.list1=list1;
        this.list2=list2;
        this.list3=list3;
    }

    /**
     * 根据Type类型来存储数据
     * @param type
     * @param list
     */
    private void addList(int type,List list){
        mPositions.put(type,types.size());
        for (int i = 0; i < list.size(); i++) {
            types.add(type);
        }
    }

    /**
     * 根据传过来的type值来选择不同的布局
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       switch (viewType){
           case DataModel.TYPE_ONE:
               return new TypeOneViewHolder(mInflater.inflate(R.layout.item_type_one,parent,false));
           case DataModel.TYPE_TWO:
               return new TypeTwoViewHolder(mInflater.inflate(R.layout.item_type_two,parent,false));
           case DataModel.TYPE_THREE:
               return new TypeThreeViewHolder(mInflater.inflate(R.layout.item_type_three,parent,false));
       }

        return null;
    }

    /**
     * 绑定viewHolder
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

//        ((TypeAbstractViewHolder)holder).bindHolder(mList.get(position));
        int viewType=getItemViewType(position);
        int realPosition=position-mPositions.get(viewType);
        switch (viewType){
            case DataModel.TYPE_ONE:
                //把每个类型的数据的第一个位置放在一个容器中
                ((TypeOneViewHolder)holder).bindHolder(list1.get(realPosition));
                break;
            case DataModel.TYPE_TWO:
            ((TypeTwoViewHolder)holder).bindHolder(list2.get(realPosition));
                break;
            case DataModel.TYPE_THREE:
            ((TypeThreeViewHolder)holder).bindHolder(list3.get(realPosition));
                break;
        }


    }

    @Override
    public int getItemViewType(int position) {
        return types.get(position);
    }

    @Override
    public int getItemCount() {
        return types.size();
    }
}
